package DAO;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Anonymous
 */
public class Connect {
    public static java.sql.Connection con;
    protected static final String PREF_TENSV = "TENSV";
    protected static final String PREF_TENCSDL = "TENCSDL";
    protected static final String PREF_TENDN = "TENDN";
    protected static final String PREF_MK = "MK";
    protected static String TenSV ; 
    protected static String TenCSDL ;
    protected static String TenDN ;
    protected static String MK ;
    protected static int portS ;
    public static boolean connectT(String tTenSV,String tTenCSDL,String tTenDN,String tMK)
    {
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://"+tTenSV+";databaseName="
                    +tTenCSDL+";user="+tTenDN+";password="+tMK;

            con = DriverManager.getConnection(connectionUrl);
            if (con != null)
            {
                Preferences prefs = Preferences.userNodeForPackage(Connect.class);
                prefs.put(PREF_TENSV, tTenSV); 
                prefs.put(PREF_TENCSDL,tTenCSDL);
                prefs.put(PREF_TENDN,tTenDN);
                prefs.put(PREF_MK,tMK);
                con.close();
                return true;
            }
            else{
                return false;
            }
            
        }
        catch (ClassNotFoundException ex)
        {
            System.out.println(ex.getMessage());
        } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public static boolean connect()
    {
        Preferences prefs = Preferences.userNodeForPackage(Connect.class);

//        prefs.put(PREF_NAME, newValue);

        //Lay thong tin server sql tu pref
        TenSV = prefs.get(PREF_TENSV, "localhost"); 
        TenCSDL = prefs.get(PREF_TENCSDL, "db_HTBVTQM");
        TenDN = prefs.get(PREF_TENDN, "");
        MK = prefs.get(PREF_MK, "");
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://"+TenSV+";databaseName="+TenCSDL+";user="
                    +TenDN+ ";password="+MK;

            con = DriverManager.getConnection(connectionUrl);
            if (con != null)
            {          
                return true;
            }
            else{
                return false;
            }
            
        }
        catch (ClassNotFoundException ex)
        {
            System.out.println(ex.getMessage());
        } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public static boolean ktKetNoi()
    {
        Preferences prefs = Preferences.userNodeForPackage(Connect.class);
        //Lay thong tin server sql tu pref
//        prefs.put(PREF_TENSV, "");
//        prefs.put(PREF_TENCSDL, "");
//        prefs.put(PREF_TENDN, "");
//        prefs.put(PREF_MK,"");
        TenSV = prefs.get(PREF_TENSV, "localhost"); 
        TenCSDL = prefs.get(PREF_TENCSDL, "db_HTBVTQM");
        TenDN = prefs.get(PREF_TENDN, "");
        MK = prefs.get(PREF_MK, "");
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://"+TenSV+";databaseName="+TenCSDL+";user="
                    +TenDN+ ";password="+MK;

            con = DriverManager.getConnection(connectionUrl);
            if (con != null)
            {          
                con.close();
                return true;
            }
            else{
                return false;
            }
            
        }
        catch (ClassNotFoundException ex)
        {
            System.out.println(ex.getMessage());
        } catch (SQLException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public static  ResultSet Xlsql(String sql)
    {
        Preferences prefs = Preferences.userNodeForPackage(Connect.class);

//        prefs.put(PREF_NAME, newValue);

        //Lay thong tin server sql tu pref
        TenSV = prefs.get(PREF_TENSV, "localhost"); 
        TenCSDL = prefs.get(PREF_TENCSDL, "db_HTBVTQM");
        TenDN = prefs.get(PREF_TENDN, "");
        MK = prefs.get(PREF_MK, "");
        ResultSet rs=null;
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://"+TenSV+";databaseName="+TenCSDL+";user="
                    +TenDN+ ";password="+MK;
            con = DriverManager.getConnection(connectionUrl);
            if(con!=null){
                Statement st = con.createStatement();
                rs = st.executeQuery(sql);           
            }
        }
        catch(SQLException ex)
            {
                System.out.println(ex.getMessage());
            } catch (ClassNotFoundException ex) {   
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }   
        return rs;
    }
    public static  boolean Xlsql2(String sql)
    {
        Preferences prefs = Preferences.userNodeForPackage(Connect.class);

//        prefs.put(PREF_NAME, newValue);

        //Lay thong tin server sql tu pref
        TenSV = prefs.get(PREF_TENSV, "localhost"); 
        TenCSDL = prefs.get(PREF_TENCSDL, "db_HTBVTQM");
        TenDN = prefs.get(PREF_TENDN, "");
        MK = prefs.get(PREF_MK, "");
        try
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://"+TenSV+";databaseName="+TenCSDL+";user="
                    +TenDN+ ";password="+MK;
            con = DriverManager.getConnection(connectionUrl);
            if(con!=null){
                Statement st = con.createStatement();
                if(st.executeUpdate(sql)==0){          
                    return false;
                }         
                con.close();
                return true;
            }
        }
        catch (SQLException ex)
        {
            System.out.println(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        
    }
     
     public static void ListenServer()
    {
        portS= 1234;
        ServerControl t1=new ServerControl(portS);
        t1.start();
     }
}
